<!DOCTYPE html>
<html>
<!--
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
<title>Place Holder Parser Tests</title>
<script src="third_party/closure/closure/goog/base.js"></script>
<script src="platform/compat.js"></script>
<script src="path.js"></script>
<script src="expression_parser.js"></script>
<script src="dependency_parser.js"></script>
<script src="place_holder_parser.js"></script>
<script>
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

function test1() {
  var p = new PlaceHolderParser;
  var result = p.parse('a{{b}}c');
  assertEquals('text', result[0].type);
  assertEquals('a', result[0].value);

  assertEquals('dep', result[1].type);
  assertEquals('b', result[1].value.path);

  assertEquals('text', result[2].type);
  assertEquals('c', result[2].value);
}

function test2() {
  var p = new PlaceHolderParser;
  var result = p.parse('{{a}}b');
  assertEquals('dep', result[0].type);
  assertEquals('a', result[0].value.path);

  assertEquals('text', result[1].type);
  assertEquals('b', result[1].value);
}

function test3() {
  var p = new PlaceHolderParser;
  var result = p.parse('a{{b}}');
  assertEquals('text', result[0].type);
  assertEquals('a', result[0].value);

  assertEquals('dep', result[1].type);
  assertEquals('b', result[1].value.path);

  assertEquals(2, result.length);
}

function test4() {
  var p = new PlaceHolderParser;
  var result = p.parse('{{a}}');
  assertEquals('dep', result[0].type);
  assertEquals('a', result[0].value.path);

  assertEquals(1, result.length);
}

function test5() {
  var p = new PlaceHolderParser;
  var result = p.parse('a');
  assertEquals('text', result[0].type);
  assertEquals('a', result[0].value);

  assertEquals(1, result.length);
}

function test6() {
  var p = new PlaceHolderParser;
  var result = p.parse('');

  assertEquals(0, result.length);
}

function test7() {
  var p = new PlaceHolderParser;
  var result = p.parse('{{ a }}');
  assertEquals('dep', result[0].type);
  assertEquals('a', result[0].value.path);

  assertEquals(1, result.length);
}

function test8() {
  var p = new PlaceHolderParser;
  var result = p.parse('a{{b');
  assertEquals('text', result[0].type);
  assertEquals('a{{b', result[0].value);
}

function test9() {
  var p = new PlaceHolderParser;
  var result = p.parse('{{a}}{{b');
  assertEquals('dep', result[0].type);
  assertEquals('a', result[0].value.path);

  assertEquals('text', result[1].type);
  assertEquals('{{b', result[1].value);
}

function test10() {
  var p = new PlaceHolderParser;
  var result = p.parse('{{a}}{{b}}');

  assertEquals(2, result.length);

  assertEquals('dep', result[0].type);
  assertEquals('a', result[0].value.path);

  assertEquals('dep', result[1].type);
  assertEquals('b', result[1].value.path);
}

function testExpression() {
  var p = new PlaceHolderParser;
  var result = p.parse('a{{ expr(b, c) b + c }}d');

  assertEquals(3, result.length);

  assertEquals('text', result[0].type);
  assertEquals('a', result[0].value);

  assertEquals('expr', result[1].type);
  assertEquals('b', result[1].value.deps[0].path.toString());
  assertEquals('c', result[1].value.deps[1].path.toString());
  assertEquals(3, result[1].value.func(1, 2));

  assertEquals('text', result[2].type);
  assertEquals('d', result[2].value);
}

function testTransform() {
  var p = new PlaceHolderParser;
  var result = p.parse('{{a | doSomething}}');
  assertEquals('dep', result[0].type);
  assertEquals('a', result[0].value.path);
  assertEquals('doSomething', result[0].value.transformName);

  assertEquals(1, result.length);
}

</script>
</body>
</html>
